﻿using System;
using System.Collections.Generic;
#if DEBUG
using System.Diagnostics;
#endif // #if DEBUG
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using FRL.DebtChased.Extensions;
using FRL.DebtChased.WorkflowObjects;
using FRL.DebtChased.WorkflowEditor.WorkflowItemEditors;
using FRL.DebtChased.Silverlight.Library;

namespace FRL.DebtChased.WorkflowEditor
{

	/// <summary>
	/// Declaration of AddActionPanel class
	/// Panel for setting up a new action to be 
	/// dragged into the planner
	/// </summary>
	public partial class AddActionPanel : UserControl
	{

		/////////////////////////////////////////////////////////////
		// Attributes

		private bool m_bLeftMouseDown = false;
		private Point m_ptMouseDownPoint;

		private WorkflowAction_Base m_action = null;

		WorkflowEditingCtrl m_parentCtrl = null;

		/////////////////////////////////////////////////////////////
		// Construction

		public AddActionPanel()
		{

			// Initialize resources
			InitializeComponent();

		}

		private void UserControl_Loaded(object sender, RoutedEventArgs e)
		{

			// Get the parent
			m_parentCtrl = (this.Parent as FrameworkElement).FindParent(typeof(WorkflowEditingCtrl)) as WorkflowEditingCtrl;

		}

		/////////////////////////////////////////////////////////////
		// Event Handlers

		private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
		{

			// Shortcuts
			if (tabCtrl == null)
				return;

			// Get the panel for the tab control
			ActionBaseCtrl actionBaseCtrl = _getSelectedCtrl();

			// Update the action from the control
			actionBaseCtrl.UpdateFromControls(ref m_action);
#if DEBUG
			Debug.Assert(m_action != null);
#endif // #if DEBUG

			// Upload the action-boing
			ActionHasChanged(m_action, sender != null);

		}

		private void butInsert_Click(object sender, RoutedEventArgs e)
		{

			// Validate the action
			ActionBaseCtrl ctrl = _getSelectedCtrl();
			List<string> Errors = ctrl.Validate();
			if (Errors != null)
			{
				MessageBoxes.PopupErrors(Errors);
				return;
			}

			// Popup the day picker
			DayPickerWnd wnd = new DayPickerWnd();
			wnd.Closed += new EventHandler(DayPickerWnd_Closed);
			wnd.Show();

		}

		void DayPickerWnd_Closed(object sender, EventArgs e)
		{

			// get the window back
			DayPickerWnd wnd = sender as DayPickerWnd;
			if (wnd.DialogResult == true)
			{

				// Generate a new action to drag
				WorkflowAction_Base newAction = WorkflowAction_Base.Clone(actionBoingCtrl.UnderlyingAction,true);

				// Create a boing for the action
				ActionBoingCtrl newBoing = new ActionBoingCtrl();
				newBoing.SetUnderlyingAction(newAction, false);

				// Hand over to the planner canvas
				m_parentCtrl.plannerCanvas.InsertActionAtDay(newBoing, DayPickerWnd.SelectedDay);

			}

		}

		private void actionBoingCtrl_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
		{

			// Update the flag
#if DEBUG
			Debug.Assert(!m_bLeftMouseDown);
#endif // #if DEBUG
			m_bLeftMouseDown = true;

			// Retain the mouse down point
			m_ptMouseDownPoint = e.GetPosition(this);

			// Capture the mouse
			actionBoingCtrl.CaptureMouse();

			// Debug message
#if DEBUG
			Debug.WriteLine("Left Mouse Button DOWN");
#endif // #if DEBUG

		}

		private void actionBoingCtrl_MouseMove(object sender, MouseEventArgs e)
		{

			// If the mouse is down...
			if (m_bLeftMouseDown)
			{

				// Calculate the amount moved...
				double dblDistance = m_ptMouseDownPoint.CalcDistance(e.GetPosition(this));

				// Debug message
#if DEBUG
				Debug.WriteLine("Moving with mouse capture, dist={0}", dblDistance);
#endif // #if DEBUG

				// If distance moved is more than 10 pixels...
				if (dblDistance > 10)
				{

					// Update dragging members
					m_bLeftMouseDown = false;

					// Release the capture
					actionBoingCtrl.ReleaseMouseCapture();

					// Debug message
#if DEBUG
					Debug.WriteLine("Triggered drag");
#endif // #if DEBUG

					// Validate the current control
					ActionBaseCtrl ctrl = _getSelectedCtrl();
					List<string> Errors = ctrl.Validate();
					if (Errors == null)
					{

						// Generate a new action to drag, and make sure the members are u2d
						WorkflowAction_Base newAction = WorkflowAction_Base.Clone(actionBoingCtrl.UnderlyingAction,true);
						ctrl.UpdateFromControls(ref newAction);

						// Create a boing for the action
						ActionBoingCtrl newBoing = new ActionBoingCtrl();
						newBoing.SetUnderlyingAction(newAction, false);

						// Hand over to the planner canvas
						m_parentCtrl.plannerCanvas.StartNewDrag(newBoing);

					}
					else
						MessageBoxes.PopupErrors(Errors);

				}

			}

		}

		private void actionBoingCtrl_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
		{

			// If the mouse is still down...
			if (m_bLeftMouseDown)
			{

				// Update the flag
#if DEBUG
				Debug.Assert(m_bLeftMouseDown);
#endif // #if DEBUG
				m_bLeftMouseDown = false;

				// Release the mouse
				actionBoingCtrl.ReleaseMouseCapture();

				// Debug message
#if DEBUG
				Debug.WriteLine("Left Mouse Button UP");
#endif // #if DEBUG

			}

		}

		/////////////////////////////////////////////////////////////
		// Operations

		private ActionBaseCtrl _getSelectedCtrl()
		{

			// Get the panel for the tab control
			TabItem tabItem = tabCtrl.SelectedItem as TabItem;
			ActionBaseCtrl actionBaseCtrl = tabItem.Content as ActionBaseCtrl;
#if DEBUG
			Debug.Assert(actionBaseCtrl != null);
#endif // #if DEBUG

			// Return variable
			return actionBaseCtrl;

		}

		public void UpdateFromCtrl(ActionBaseCtrl ctrl)
		{
#if DEBUG
			Debug.Assert(!ctrl.BlockUpdateEvents);
#endif // #if DEBUG

			// Update the action object
			ctrl.UpdateFromControls(ref m_action);

			// Update the boing
			actionBoingCtrl.SetUnderlyingAction(m_action, true);

		}

		public void ActionHasChanged(WorkflowAction_Base action, bool bAnimate=true)
		{
#if DEBUG
			Debug.Assert(action!=null);
#endif // #if DEBUG

			// Update the action-boing
			actionBoingCtrl.SetUnderlyingAction(action, bAnimate);

		}

		public void SupportDataLoaded()
		{

			// Called after plug-in has loaded support data

			// Initialize the tab controls
			phoneCallCtrl.InitializeControls();
			sendLetterFaxCtrl.InitializeControls();
			sendEmailCtrl.InitializeControls();
			sendSMSCtrl.InitializeControls();
			popupCtrl.InitializeControls();
			searchCtrl.InitializeControls();
			notifyManagerCtrl.InitializeControls();
			manualCtrl.InitializeControls();

			// Update the boing-action
			TabControl_SelectionChanged(null, null);

		}

	}

}
